/*global pi, ArrayBuffer */

/**
 * 线程间使用的字节缓冲区，目前仅用于t3d模块
 */

pi.mod.define('pi.cbuffer', function (pi) {
	"use strict";
	var module = {},
		vcalls = [
			/**
			 * 创建bytes
			 * 返回：int, 字节缓冲区的句柄
			 */
			["pi_bytes_new", pi.vcall.TYPE_INT, [], "create"],

			/**
			 * 释放缓冲区
			 * bytes：int，字节缓冲区的句柄
			 */
			// module.destroy(bytes);
			["pi_bytes_free", pi.vcall.TYPE_VOID, [pi.vcall.TYPE_INT], "destroy"],

			/**
			 * 清除缓冲区内容
			 * bytes: int, 字节缓冲区的句柄
			 */
			// module.clear(bytes);
			["pi_bytes_clear", pi.vcall.TYPE_VOID, [pi.vcall.TYPE_INT], "clear"],

			/**
			 * 往字节缓冲中写入8位整数
			 * bytes：int，字节缓冲区的句柄
			 * num: int, -128到127之间的整数
			 */
			// module.writeInt8(bytes, num);
			["pi_bytes_write_int8", pi.vcall.TYPE_VOID, [pi.vcall.TYPE_INT, pi.vcall.TYPE_INT], "writeInt8"],

			/**
			 * 往字节缓冲中写入16位整数
			 * bytes：int，字节缓冲区的句柄
			 * num: int，-32768到32767之间的整数
			 */
			// module.writeInt16 (bytes, num)
			["pi_bytes_write_int16", pi.vcall.TYPE_VOID, [pi.vcall.TYPE_INT, pi.vcall.TYPE_INT]],

			/**
			 * 往字节缓冲中写入32位整数
			 * bytes：int，字节缓冲区的句柄
			 * num: int，32位整数
			 */
			// module.writeInt(bytes, num)
			["pi_bytes_write_int", pi.vcall.TYPE_VOID, [pi.vcall.TYPE_INT, pi.vcall.TYPE_INT], "writeInt"],

			/**
			 * 从字节缓冲读取一个32位整数
			 */
			// module.readInt (bytes)
			["pi_bytes_read_int_unsafe", pi.vcall.TYPE_INT, [pi.vcall.TYPE_INT], "readInt"],

			/**
			 * 往字节缓冲中写入32位浮点数
			 * bytes：int，字节缓冲区的句柄
			 * num: float，浮点数
			 */
			// module.writeFloat(bytes, num)
			["pi_bytes_write_float", pi.vcall.TYPE_VOID, [pi.vcall.TYPE_INT, pi.vcall.TYPE_FLOAT], "writeFloat"],

			/**
			 * 从字节缓冲区读取32位浮点数
			 */
			// module.readFloat (bytes)
			["pi_bytes_read_float_unsafe", pi.vcall.TYPE_DOUBLE, [pi.vcall.TYPE_INT], "readFloat"],

			/**
			 * 从字节缓冲区读取64位浮点数
			 */
			// module.readFloat (bytes)
			["pi_bytes_read_double_unsafe", pi.vcall.TYPE_DOUBLE, [pi.vcall.TYPE_INT], "readDouble"],

			/**
			 * 往字节缓冲中写入64位浮点数
			 * bytes：int，字节缓冲区的句柄
			 * num: double，浮点数
			 */
			// module.writeDouble(bytes, num)
			["pi_bytes_write_double", pi.vcall.TYPE_VOID, [pi.vcall.TYPE_INT, pi.vcall.TYPE_DOUBLE], "writeDouble"],

			/**
			 * 往字节缓冲中写入指针
			 * bytes：int，字节缓冲区的句柄
			 * handle: int，指针
			 */
			// module.writePointer(bytes, handle);
			["pi_bytes_write_pointer", pi.vcall.TYPE_VOID, [pi.vcall.TYPE_INT, pi.vcall.TYPE_INT], "writePointer"],

			/**
			 * 往字节缓冲中写入字符串
			 * bytes：int，字节缓冲区的句柄
			 * str: string, 字符串
			 */
			// module.writeString(bytes, str)
			["pi_bytes_write_wstr", pi.vcall.TYPE_VOID, [pi.vcall.TYPE_INT, pi.vcall.TYPE_STRING], "writeString"],

			/**
			 * 往字节缓冲中写入指定字节数的二进制数据
			 * bytes：int，字节缓冲区的句柄
			 * data: ArrayBuffer或者底层句柄
			 * size: data的字节数
			 */
			// module.writeData(bytes, data, size)
			["pi_bytes_write_data", pi.vcall.TYPE_VOID, [pi.vcall.TYPE_INT, pi.vcall.TYPE_POINTER, pi.vcall.TYPE_INT], "writeData"],

			/**
			 * 往字节缓冲中写入指定字节数的二进制数据，连同长度
			 * bytes：int，字节缓冲区的句柄
			 * data: ArrayBuffer或者底层句柄
			 * size: data的字节数
			 */
			// module.writeBinary(bytes, data, size)
			["pi_bytes_write_binary", pi.vcall.TYPE_VOID, [pi.vcall.TYPE_INT, pi.vcall.TYPE_POINTER, pi.vcall.TYPE_INT], "writeBinary"],
			/**
			 * 往字节缓冲中写入另一个缓冲区的数据，不带长度
			 * dst：int，字节缓冲区的句柄
			 * src：int，字节缓冲区的句柄
			 */
			// module.writeBuffer = vcalls.pi_bytes_copy;
			["pi_bytes_copy", pi.vcall.TYPE_VOID, [pi.vcall.TYPE_INT, pi.vcall.TYPE_INT], "writeBuffer"]
		];


	// 注册所有 vcall 函数句柄
	pi.vcall.makeVCall("pi_lib.dll", module, vcalls);

	return module;
});